"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 导入导出、备份恢复工具支持M*兼容性
Case Name   : 导入导出兼容M*修改表空间语法
Create At   : 2023.05.04
Owner       : z@wan005
Description :
    1.创建兼容b库
    2.兼容b库下修改表空间并添加wait和engine选项 
    3.使用gs_dumpall工具导出
    4.删除表空间
    5.gsql导入
    6.连接导入库查询表空间
    7.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.成功
    5.成功
    6.成功
    7.成功
History     :
"""

import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class GsDump0046(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.dbuser_node = Node('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = "db_dump_0046"
        self.ts_name = "ts_dump_0046"
        self.dump_path = os.path.join(macro.DB_BACKUP_PATH, 'dump0046')

    def test_server_tools(self):
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        text = '----step1: 创建兼容b库 expect: 成功----'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} dbcompatibility 'b';"
        self.log.info(sql_cmd)
        sql_result = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_result)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_result,
                      '执行失败:' + text)

        text = '----step2: 兼容b库下修改表空间并添加wait和engine选项 expect: 成功----'
        self.log.info(text)
        sql_cmd = f'''drop tablespace if exists {self.ts_name};
        create tablespace {self.ts_name} add datafile 'test3.ibd';
        alter tablespace {self.ts_name} rename to {self.ts_name}_01 \
        engine = InnoDB;
        alter tablespace {self.ts_name}_01 rename to {self.ts_name}_02 wait;'''
        self.log.info(sql_cmd)
        sql_result = self.sh_primary.execut_db_sql(sql_cmd,
                                                   dbname=self.db_name)
        self.log.info(sql_result)
        self.assertIn(self.constant.TABLESPCE_CREATE_SUCCESS, sql_result,
                      '执行失败:' + text)
        self.assertIn(self.constant.TABLESPCE_ALTER_SUCCESS, sql_result,
                      '执行失败:' + text)

        text = '----step3: 使用gs_dumpall工具导出 expect: 成功----'
        self.log.info(text)
        gs_dump_cmd = f"source {macro.DB_ENV_PATH};" \
                      f"gs_dumpall " \
                      f"-p {self.dbuser_node.db_port} " \
                      f"-f {self.dump_path}"
        self.log.info(gs_dump_cmd)
        dump_msg = self.dbuser_node.sh(gs_dump_cmd).result()
        self.log.info(dump_msg)
        self.assertIn(f" dump database dbname='{self.db_name}' successfully",
                      dump_msg, '执行失败:' + text)

        text = '----step4: 删除表空间 expect: 成功----'
        self.log.info(text)
        sql_cmd = f'''drop tablespace if exists {self.ts_name}_02;'''
        self.log.info(sql_cmd)
        sql_result = self.sh_primary.execut_db_sql(sql_cmd,
                                                   dbname=self.db_name)
        self.log.info(sql_result)
        self.assertIn(self.constant.TABLESPCE_DROP_SUCCESS, sql_result,
                      '执行失败:' + text)

        text = '----step5: gsql导入 expect: 成功----'
        self.log.info(text)
        gs_restore_cmd = f"source {macro.DB_ENV_PATH};" \
                         f"gsql -p {self.dbuser_node.db_port} " \
                         f"-d {self.db_name} -f {self.dump_path}"
        self.log.info(gs_restore_cmd)
        restore_msg = self.dbuser_node.sh(gs_restore_cmd).result()
        self.log.info(restore_msg)
        self.assertIn(f'You are now connected to database "{self.db_name}"',
                      restore_msg, '执行失败:' + text)

        text = '----step6: 连接导入库查询表空间 expect:导入成功----'
        self.log.info(text)
        sql_cmd = f"select spcname, spcoptions from pg_tablespace where " \
                  f"spcname like '{self.ts_name}%' order by spcname;"
        self.log.info(sql_cmd)
        sql_result = self.sh_primary.execut_db_sql(sql_cmd,
                                                   dbname=self.db_name)
        self.log.info(sql_result)
        self.assertIn('ts_dump_0046_02', sql_result, '执行失败:' + text)

    def tearDown(self):
        text = '---step7: 清理环境 expect: 成功----'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"drop tablespace if exists {self.ts_name}_02;"
        self.log.info(sql_cmd)
        sql_result = self.sh_primary.execut_db_sql(sql_cmd,
                                                   dbname='postgres')
        self.log.info(sql_result)
        rm_cmd = f"rm -rf {self.dump_path}"
        self.log.info(rm_cmd)
        rm_msg = self.dbuser_node.sh(rm_cmd).result()
        self.log.info(rm_msg)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, sql_result,
                      '执行失败:' + text)
        self.assertIn(self.constant.TABLESPCE_DROP_SUCCESS, sql_result,
                      '执行失败:' + text)
        self.assertEqual('', rm_msg)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
